﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers;
using eTMS.Controllers.BoMonService;
using eTMS.Controllers.HocKyService;
using eTMS.Controllers.KhoaService;
using eTMS.Controllers.MonHocService;
using eTMS.Controllers.NamHocService;
using eTMS.Controllers.NganhHocService;

namespace eTMS.UserControls
{
    public partial class TreeKhoaBoMonMonHocLopTinChi : eTMS.Controls.UserControlBase
    {
        public TreeKhoaBoMonMonHocLopTinChi()
        {
            InitializeComponent();
        }

        public override bool AllowDrop
        {
            get
            {
                return base.AllowDrop;
            }
            set
            {
                base.AllowDrop = value;
                treeView1.AllowDrop = value;
            }
        }

        public event TreeViewEventHandler OnCheckedChanged;
        public MonHocEntity GetSelectedMonHoc()
        {
            if (treeView1.SelectedNode.Tag is MonHocEntity)
                return (MonHocEntity)treeView1.SelectedNode.Tag;
            return null;
        }
        public List<int> GetListSelectedBoMon()
        {
            List<int> lsID = new List<int>();
            if (treeView1.SelectedNode != null)
            {
                if (treeView1.SelectedNode.Tag is BoMonEntity)
                {
                    lsID.Add(((BoMonEntity)treeView1.SelectedNode.Tag).BoMon_ID);
                }
                else if (treeView1.SelectedNode.Tag is KhoaEntity)
                {
                    foreach (TreeNode node in treeView1.SelectedNode.Nodes)
                    {
                        if (node.Tag is BoMonEntity)
                        {
                            lsID.Add(((BoMonEntity)node.Tag).BoMon_ID);
                        }
                    }
                }
            }
            return lsID;
        }
        public int GetSelectedMonHoc_ID()
        {
            if (treeView1.SelectedNode == null) return 0;
            if (treeView1.SelectedNode.Tag is MonHocEntity)
                return ((MonHocEntity)treeView1.SelectedNode.Tag).MonHoc_ID;
            if (treeView1.SelectedNode.Tag is DataRow)
                return (int)((DataRow)treeView1.SelectedNode.Tag)["MonHoc_ID"];
            return 0;
        }

        public int GetSelectedLopTinChi_ID()
        {
            if (treeView1.SelectedNode == null) return 0;
            if (treeView1.SelectedNode.Tag is DataRow)
                return (int)((DataRow)treeView1.SelectedNode.Tag)["LopTinChi_ID"];
            return 0;
        }
        public List<MonHocEntity> GetSelectedMonHocChilds()
        {
            List<MonHocEntity> ls = new List<MonHocEntity>();
            AddMonHocEntityToList(treeView1.SelectedNode, ls);
            return ls;
        }
        private void AddMonHocEntityToList(TreeNode node, List<MonHocEntity> ls)
        {
            if (treeView1.SelectedNode.Tag is MonHocEntity)
                ls.Add((MonHocEntity)treeView1.SelectedNode.Tag);
            else
            {
                foreach (TreeNode treeNode in node.Nodes)
                {
                    AddMonHocEntityToList(treeNode, ls);
                }
            }
        }
        public NamHocEntity GetSelectedNamHoc()
        {
            return (NamHocEntity)comboBoxNamHoc1.SelectedItem;
        }
        public HocKyEntity GetSelectedHocKy()
        {
            return (HocKyEntity)comboBoxHocKy1.SelectedItem;
        }
        public event EventHandler OnSelectedChanged;
        private bool _IsShowBoMon = false;
        private bool _IsShowLopTinChi = false;
        private bool _IsShowMonHoc = true;
        public bool IsShowNamHocHocKy
        {
            get
            {
                return groupBox1.Visible;
            }
            set
            {
                groupBox1.Visible = value;
            }
        }
        public bool IsShowCheck
        {
            get
            {
                return treeView1.CheckBoxes;
            }
            set
            {
                treeView1.CheckBoxes = value;
            }
        }
        public bool IsShowBoMon
        {
            get
            {
                return _IsShowBoMon;
            }
            set
            {
                _IsShowBoMon = value;
            }
        }
        public bool IsShowLopTinChi
        {
            get
            {
                return _IsShowLopTinChi;
            }
            set
            {
                _IsShowLopTinChi = value;
            }
        }
        public bool IsShowMonHoc
        {
            get
            {
                return _IsShowMonHoc;
            }
            set
            {
                _IsShowMonHoc = value;
            }
        }
        public int HocKy_ID
        {
            get
            {
                return (int)comboBoxHocKy1.SelectedValue;
            }
        }
        public int NamHoc_ID
        {
            get
            {
                return (int)comboBoxNamHoc1.SelectedValue;
            }
        }
        public bool IsAllowDrag
        {
            set
            {
                treeView1.AllowDrop = value;
            }
            get
            {
                return treeView1.AllowDrop;
            }
        }

        private void DisplayTree()
        {
            treeView1.Nodes.Clear();
            TreeNode root = new TreeNode(eTMS.Commons.Constants.APP_ALL);
            KhoaController.LoadAllKhoa(false);
            if (_IsShowMonHoc)
            {
                MonHocController.LoadAllMonHoc(false);
            }
            BoMonController.LoadAllBoMon(false);
            root.ImageIndex = 0;
            foreach (KhoaEntity khoaEntity in KhoaController.ListKhoaStatic)
            {
                TreeNode node = new TreeNode();
                node.Text = khoaEntity.TenKhoa;
                node.Tag = khoaEntity;
                node.ImageIndex = 1;
                root.Nodes.Add(node);
                List<BoMonEntity> listBoMon = BoMonController.ListBoMonStatic.FindAll(delegate(BoMonEntity oFind) { return oFind.Khoa_ID.Equals(khoaEntity.Khoa_ID); });
                if (_IsShowBoMon)
                {
                    foreach (BoMonEntity boMonEntity in listBoMon)
                    {
                        TreeNode subMode = new TreeNode();
                        subMode.Tag = boMonEntity;
                        subMode.Text = boMonEntity.TenBoMon;
                        subMode.ImageIndex = 2;
                        node.Nodes.Add(subMode);
                        if (_IsShowMonHoc)
                        {
                            List<MonHocEntity> lsMonHoc = MonHocController.ListMonHocStatic.FindAll(delegate(MonHocEntity oFind) { return oFind.BoMon_ID.Equals(boMonEntity.BoMon_ID); });
                            AddMonHocNode(subMode, lsMonHoc);
                        }
                    }
                }
                else
                {
                    if (_IsShowMonHoc)
                    {
                        List<MonHocEntity> lsMonHoc = MonHocController.ListMonHocStatic.FindAll(delegate(MonHocEntity oFind) { return listBoMon.Find(delegate(BoMonEntity oBoMon) { return oFind.BoMon_ID.Equals(oBoMon.BoMon_ID); }) != null; });
                        AddMonHocNode(node, lsMonHoc);
                    }
                }
            }
            treeView1.Nodes.Add(root);
            root.Expand();
        }

        private LopTinChiController lopTinChiController;
        private void AddMonHocNode(TreeNode parentNode, List<MonHocEntity> lsMonHoc)
        {
            foreach (MonHocEntity monHocEntity in lsMonHoc)
            {
                TreeNode node = new TreeNode(monHocEntity.MaMonHoc + ":" + monHocEntity.TenMonHoc);
                node.ImageIndex = 3;
                node.Tag = monHocEntity;
                parentNode.Nodes.Add(node);
                if (_IsShowLopTinChi)
                {
                    foreach (DataRow rowLopTinChi in lopTinChiController.Table.Select("MonHoc_ID=" + monHocEntity.MonHoc_ID.ToString()))
                    {
                        TreeNode sub = new TreeNode(rowLopTinChi["MaLop"].ToString());
                        sub.ImageIndex = 4;
                        sub.Tag = rowLopTinChi;
                        node.Nodes.Add(sub);
                    }
                }
            }
        }
        public DataRow SelectLopTinChi(int LopTinChi_ID)
        {
            DataRow[] rs = lopTinChiController.Table.Select("LopTinChi_ID=" + LopTinChi_ID.ToString());
            if (rs.Length > 0) return rs[0];
            return null;
        }
        private void TreeKhoaBoMonMonHocLopTinChi_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
            if (_IsShowLopTinChi && lopTinChiController == null)
            {
                lopTinChiController = new LopTinChiController();
            }
            if (IsShowNamHocHocKy)
            {
                NamHocController.LoadAllNamHoc(false);
                HocKyController.LoadAllHocKy(false);
                comboBoxNamHoc1.BindData();
                comboBoxHocKy1.BindData();
            }
            if (!_IsShowLopTinChi)
            {
                DisplayTree();
            }
        }

        private void comboBoxNamHoc1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (DesignMode) return;
            if (comboBoxHocKy1.SelectedValue == null) return;
            if (comboBoxNamHoc1.SelectedValue == null) return;
            if (_IsShowLopTinChi)
            {
                lopTinChiController.LoadAllDSLopTinChiByHocKyNamHoc((int)comboBoxHocKy1.SelectedValue, (int)comboBoxNamHoc1.SelectedValue, new List<int>());
                DisplayTree();
            }
        }
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (OnSelectedChanged != null)
            {
                OnSelectedChanged(sender, new EventArgs());
            }
        }

        private bool IsInternal = false;
        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            bool isFirst = false;
            if (!IsInternal)
            {
                isFirst = true;
                if (OnCheckedChanged != null)
                {
                    OnCheckedChanged(sender, e);
                }
            }
            foreach (TreeNode node in e.Node.Nodes)
            {
                IsInternal = true;
                node.Checked = e.Node.Checked;
            }
            if (isFirst)
                IsInternal = false;
        }
        public List<int> GetListCheckedMonHoc_ID()
        {
            List<TreeNode> ls = new List<TreeNode>();
            List<int> lsID = new List<int>();
            foreach (TreeNode node in this.treeView1.Nodes)
            {
                if (!this.IsShowBoMon)
                {
                    foreach (TreeNode sNode in node.Nodes)
                    {

                        foreach (TreeNode ssNode in sNode.Nodes)
                        {
                            ls.Add(ssNode);
                        }
                    }
                }
                else
                {
                    foreach (TreeNode sNode in node.Nodes)
                    {
                        foreach (TreeNode ssNode in sNode.Nodes)
                        {
                            foreach (TreeNode sssNode in node.Nodes)
                            {
                                ls.Add(sssNode);
                            }
                        }
                    }
                }
            }
            foreach (TreeNode node in ls)
            {
                bool isChecked = false;
                foreach (TreeNode sNode in node.Nodes)
                {
                    if (sNode.Checked)
                    {
                        isChecked = true;
                        break;
                    }
                }
                if (isChecked || node.Checked)
                {
                    lsID.Add(((MonHocEntity)node.Tag).MonHoc_ID);
                }
            }
            return lsID;
        }
        private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            if (e.Item is TreeNode)
            {
                TreeNode node = (TreeNode)e.Item;
                if (node.Tag is DataRow)
                {
                    treeView1.DoDragDrop(node, DragDropEffects.All);
                }
            }

        }
        public List<int> GetLsLopTinChi()
        {
            List<int> ls = new List<int>();
            if (lopTinChiController.Table == null) return ls;
            foreach (DataRow row in lopTinChiController.Table.Rows)
            {
                ls.Add(lopTinChiController.ConvertInt(row["LopTinChi_ID"]));
            }
            return ls;
        }

        //public List<EduProgramGroup> GetListEduProgramGroup()
        //{
        //    List<EduProgramGroup> ls = new List<EduProgramGroup>();

        //    foreach (TreeNode node in this.treeView1.Nodes[0].Nodes)
        //    {
        //        if (!this.IsShowBoMon)
        //        {
        //            EduProgramGroup eduProgramGroup = new EduProgramGroup();
        //            KhoaEntity khoaEntity = (KhoaEntity)node.Tag;
        //            eduProgramGroup.ID = khoaEntity.Khoa_ID;
        //            foreach (TreeNode sNode in node.Nodes)
        //            {
        //                EduProgram eduProgram = new EduProgram();
        //                MonHocEntity monHocEntity = (MonHocEntity)sNode.Tag;
        //                eduProgram.ID = monHocEntity.MonHoc_ID;
        //                foreach (TreeNode ssNode in sNode.Nodes)
        //                {
        //                    if (ssNode.Checked)
        //                    {
        //                        DataRow row = (DataRow)ssNode.Tag;
        //                        Course course = new Course();
        //                        course.Parent = eduProgram;
        //                        course.ID = Convert.ToInt32(row["LopTinChi_ID"]);
        //                        eduProgram.Childs.Add(course);
        //                    }
        //                }
        //                if (eduProgram.Childs.Count > 0)
        //                {
        //                    eduProgram.Parent = eduProgramGroup;
        //                    eduProgramGroup.Childs.Add(eduProgram);
        //                }
        //            }
        //            if (eduProgramGroup.Childs.Count > 0)
        //            {

        //                ls.Add(eduProgramGroup);
        //            }
        //        }
        //        //else
        //        //{
        //        //    foreach (TreeNode sNode in node.Nodes)
        //        //    {
        //        //        foreach (TreeNode ssNode in sNode.Nodes)
        //        //        {
        //        //            foreach (TreeNode sssNode in node.Nodes)
        //        //            {
        //        //                ls.Add(sssNode);
        //        //            }
        //        //        }
        //        //    }
        //        //}
        //    }

        //    return ls;
        //}
        
        public event DragEventHandler TreeDragEnter;
        public event DragEventHandler TreeDragOver;
        public event DragEventHandler TreeDragDrop;
        private void treeView1_DragEnter(object sender, DragEventArgs e)
        {
            if (TreeDragEnter != null)
            {
                TreeDragEnter(sender, e);
            }
        }
        public TreeNode GetNodeAt(Point point)
        {
            return treeView1.GetNodeAt(treeView1.PointToClient(point));
        }
        private void treeView1_DragOver(object sender, DragEventArgs e)
        {
            if (TreeDragOver != null)
            {
                TreeDragOver(sender, e);
            }

        }

        private void treeView1_DragDrop(object sender, DragEventArgs e)
        {
            if (TreeDragDrop != null)
            {
                TreeDragDrop(sender, e);
            }
        }

    }
}
